package com.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.travel.common.Result;


import com.travel.domain.entity.ScenicProfile;
import com.travel.service.ScenicProfileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/scenicProfile")
public class ScenicProfileController {
    @Autowired
    private ScenicProfileService service;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    private static final String SCENIC_PROFILE_CACHE_KEY = "scenicProfiles";
    private static final String SCENIC_FILTER_CACHE_KEY = "scenic:filter:";
    private static final String CACHE_KEY_TOP15 = "scenic:top15";  // 默认 15 条数据的缓存键
    @GetMapping("/getProfileList")
    public Result<List<ScenicProfile>> getProfile(){
        // 先从缓存中获取
        List<ScenicProfile> scenicProfiles = (List<ScenicProfile>) redisTemplate.opsForValue().get(SCENIC_PROFILE_CACHE_KEY);
        if (scenicProfiles != null) {
            return Result.success(scenicProfiles);
        }
        try {
            List<ScenicProfile> scenicList = service.listAllProfiles();
            // 将查询结果缓存起来，设置过期时间为1小时
            redisTemplate.opsForValue().set(SCENIC_PROFILE_CACHE_KEY, scenicList, 1, TimeUnit.HOURS);
            return Result.success(scenicList);
        } catch (Exception e) {
            log.error("获取景区列表失败: {}", e.getMessage());
            return Result.error("获取景区列表失败");
        }
    }
    @GetMapping("/scenic-filter")
    public Result<List<ScenicProfile>> getScenicFilter(
            @RequestParam(required = false) Long typeId,   // 使用 typeId 进行筛选
            @RequestParam(required = false) Long areaId,    // 使用 areaId 进行筛选
            @RequestParam(required = false) String name      // 模糊查询名称
    ) {
        // 根据筛选条件构建一个唯一的缓存键
        String cacheKey = SCENIC_FILTER_CACHE_KEY + (typeId != null ? typeId : "null") + ":"
                + (areaId != null ? areaId : "null") + ":"
                + (name != null ? name : "null");
        try {
            // 尝试从缓存中获取数据
            List<ScenicProfile> cachedList = (List<ScenicProfile>) redisTemplate.opsForValue().get(cacheKey);
            if (cachedList != null) {
                // 如果缓存中有数据，直接返回缓存数据
                return Result.success(cachedList);
            }
            // 如果没有任何筛选条件，返回默认的15条数据
            if (typeId == null && areaId == null && (name == null || name.isEmpty())) {
                // 检查默认的 15 条数据是否已缓存
                List<ScenicProfile> top15 = (List<ScenicProfile>) redisTemplate.opsForValue().get(CACHE_KEY_TOP15);
                if (top15 != null) {
                    // 如果缓存中有默认的 15 条数据，直接返回缓存数据
                    return Result.success(top15);
                }
                top15 = service.getTop15ByDistance();
                // 将查询结果存入缓存，并设置过期时间（如 5 分钟）
                redisTemplate.opsForValue().set(CACHE_KEY_TOP15, top15, 60, TimeUnit.MINUTES);
                return Result.success(top15);
            }

            // 否则进行筛选
            List<ScenicProfile> filteredList = service.filterScenicProfiles(typeId, areaId, name);
            // 将查询结果存入缓存，并设置过期时间（如 5 分钟）
            redisTemplate.opsForValue().set(cacheKey, filteredList, 60, TimeUnit.MINUTES);
            return Result.success(filteredList);
        } catch (Exception e) {
            // 捕获异常并返回统一的错误响应
            return Result.error("服务器内部错误：" + e.getMessage());
        }
    }
}
